/**
 * @author        hejiyang <silentmanager@163.com>
 * @date          2024-12-16 09:44:24
 * Copyright © YourCompanyName All rights reserved
 */

import { ref } from 'vue'
import { simpleListDept } from '@/api/customer'

export function useSimpleDept() {
  const deptList = ref<{ label: string; value: number }[]>([])
  const loading = ref(false) // 用于控制加载状态
  const error = ref<Error | null>(null) // 存储错误信息

  const fetchDeptList = async () => {
    loading.value = true
    try {
      const data = await simpleListDept()
      deptList.value = data.map((item) => {
        return { label: item.name, value: item.id }
      })
      error.value = null
    } catch (e) {
      console.error(e)
      error.value = e as Error
    } finally {
      loading.value = false
    }
  }

  // 初始化时自动加载部门列表
  fetchDeptList()

  return {
    deptList,
    loading,
    error,
    fetchDeptList
  }
}
